﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using VIP.Extension;

public class VIPUIEditor : MonoBehaviour {


    [MenuItem("GameObject/VIP/VIPVoice", priority = 0)]
    public static void VIPVoice()
    {
        GameObject panelGo = new GameObject("VIPVoice");
        VIPUndo.CreateObjectUndo("Create Panel Go", panelGo);
        //设置父物体
        GameObject selectGo = Selection.activeGameObject;
        if (selectGo.IsNotNull()) {
            SetParent(panelGo.transform, selectGo.transform);
        }
        //选中定位
        Selection.activeGameObject = panelGo;
    }

    /// <summary>
    /// 创建VIPCanvas(UIManager用的)
    /// </summary>
    [MenuItem("GameObject/VIP/VIPUI/VIPCanvas")]
    public static void VIPCanvas()
    {
        Canvas canvas = CreateVIPCanvas();
        //设置父物体
        GameObject selectGo = Selection.activeGameObject;
        if (selectGo.IsNotNull())
        {
            SetParent(canvas.GetComponent<RectTransform>(), selectGo.transform);
        }
        //选中定位
        Selection.activeGameObject = canvas.gameObject;
    }

    /// <summary>
    /// 创建 (进入/退出/按下/抬起) 按钮
    /// </summary>
    [MenuItem("GameObject/VIP/VIPUI/VIPButton")]
    public static void VIPButton() {
        Canvas canvas = FindObjectOfType<Canvas>();
        if (canvas.IsNull()) {
            canvas = CreateVIPCanvas();
        }
        string uiText = "(进入/退出/按下/抬起/长按/双击)按钮";
        GameObject newButtonGo = new GameObject("VIPButton");
        RectTransform newButtonRect = newButtonGo.AddComponent<RectTransform>();
        newButtonRect.sizeDelta = new Vector2(260, 30);
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create VIPButton Go", newButtonGo);
        //创建Text对象
        CreateButtonText(uiText, newButtonGo.transform);
        newButtonGo.AddComponent<Image>();
        //TODO 获取到系统中的UISprite设置到buttonImage上
        newButtonGo.AddComponent<VIPButton>();
        //设置父物体
        SetParent(newButtonRect, canvas);
        //选中定位
        Selection.activeGameObject = newButtonGo;
    }

    /// <summary>
    /// 创建 (选中事件) 输入框
    /// </summary>
    [MenuItem("GameObject/VIP/VIPUI/VIPInputField")]
    public static void VIPInputField() {
        Canvas canvas = FindObjectOfType<Canvas>();
        if (canvas.IsNull())
        {
            canvas = CreateVIPCanvas();
        }
        string uiPlaceholder = "Enter text...";
        string uiText = "输入框(带选择回调)";
        GameObject newInputFieldGo = new GameObject("VIPInputField");
        RectTransform newButtonRect = newInputFieldGo.AddComponent<RectTransform>();
        newButtonRect.sizeDelta = new Vector2(160, 30);
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create VIPInputField Go", newInputFieldGo);
        //创建Text对象
        Text placeholder = CreateInputFieldPlaceholder(uiPlaceholder, newInputFieldGo.transform);
        Text text = CreateInputFieldText(uiText, newInputFieldGo.transform);
        newInputFieldGo.AddComponent<Image>();
        //TODO 获取到系统中的UISprite设置到buttonImage上
        VIPInputField vipInputField = newInputFieldGo.AddComponent<VIPInputField>();
        vipInputField.textComponent = text;
        vipInputField.placeholder = placeholder;
        //设置父物体
        SetParent(newButtonRect, canvas);
        //选中定位
        Selection.activeGameObject = newInputFieldGo;
    }

    /// <summary>
    /// 创建 (滑动结束回调，元素定位) 列表
    /// </summary>
    [MenuItem("GameObject/VIP/VIPUI/VIPScrollView")]
    public static void VIPScrollView() {
        //创建预制体
        Canvas canvas = FindObjectOfType<Canvas>();
        if (canvas.IsNull())
        {
            canvas = CreateVIPCanvas();
        }
        GameObject vipScrollViewPrefab = Resources.Load("VIPUI/VIPScrollView") as GameObject;
        GameObject vipScrollViewGo = Instantiate(vipScrollViewPrefab);
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create VIPScrollView Go", vipScrollViewGo);
        vipScrollViewGo.name = "VIPScrollView";
        RectTransform vipScrollViewRect = vipScrollViewGo.GetComponent<RectTransform>();
        //设置父物体
        SetParent(vipScrollViewRect, canvas);
        //选中定位
        Selection.activeGameObject = vipScrollViewGo;
    }

    /// <summary>
    /// 创建VIPCanvas
    /// </summary>
    /// <returns></returns>
    private static Canvas CreateVIPCanvas() {
        Canvas canvas = FindObjectOfType<Canvas>();
        //如果编辑器中没找到Canvas才创建
        GameObject newCanvasGo = new GameObject("Canvas");
        newCanvasGo.AddComponent<RectTransform>();
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create Canvas Go", newCanvasGo);
        canvas = newCanvasGo.AddComponent<Canvas>();
        canvas.name = "VIPCanvas";
        canvas.renderMode = RenderMode.ScreenSpaceOverlay;
        newCanvasGo.AddComponent<CanvasScaler>();
        newCanvasGo.AddComponent<GraphicRaycaster>();
        //创建Panel
        GameObject panelGo = new GameObject("Panel");
        VIPUndo.CreateObjectUndo("Create Panel Go", panelGo);
        RectTransform panelRect = panelGo.AddComponent<RectTransform>();
        panelRect.SetParent(canvas.transform, false);
        panelRect.offsetMin = Vector2.zero;
        panelRect.offsetMax = Vector2.zero;
        panelRect.anchorMin = Vector2.zero;
        panelRect.anchorMax = Vector2.one;
        //创建Tip
        GameObject tipGo = new GameObject("Tip");
        VIPUndo.CreateObjectUndo("Create Tip Go", tipGo);
        RectTransform tipRect = tipGo.AddComponent<RectTransform>();
        tipRect.SetParent(canvas.transform, false);
        tipRect.offsetMin = Vector2.zero;
        tipRect.offsetMax = Vector2.zero;
        tipRect.anchorMin = Vector2.zero;
        tipRect.anchorMax = Vector2.one;
        CreateEventSystem();
        return canvas;
    }

    /// <summary>
    /// 创建Canvas
    /// </summary>
    private static Canvas CreateCanvas() {
        Canvas canvas = FindObjectOfType<Canvas>();
        //如果编辑器中没找到Canvas才创建
        GameObject newCanvasGo = new GameObject("Canvas");
        newCanvasGo.AddComponent<RectTransform>();
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create Canvas Go", newCanvasGo);
        canvas = newCanvasGo.AddComponent<Canvas>();
        canvas.renderMode = RenderMode.ScreenSpaceOverlay;
        newCanvasGo.AddComponent<CanvasScaler>();
        newCanvasGo.AddComponent<GraphicRaycaster>();
        CreateEventSystem();
        return canvas;
    }

    /// <summary>
    /// 创建EventSystem
    /// </summary>
    private static void CreateEventSystem() {
        EventSystem eventSystem = FindObjectOfType<EventSystem>();
        //如果编辑器中没找到EventSystem才创建
        if (null == eventSystem)
        {
            GameObject newEventSystemGo = new GameObject("EventSystem");
            //创建撤销点
            VIPUndo.CreateObjectUndo("Create EventSystem Go", newEventSystemGo);
            eventSystem = newEventSystemGo.AddComponent<EventSystem>();
            newEventSystemGo.AddComponent<StandaloneInputModule>();
        }
    }

    /// <summary>
    /// 创建一个Image对象
    /// </summary>
    /// <param name="parent"></param>
    /// <returns></returns>
    private static Image CreateImage(Transform parent = null) {
        GameObject newImageGo = new GameObject("Image");
        newImageGo.AddComponent<RectTransform>();
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create Image Go", newImageGo);
        if (null != parent) {
            newImageGo.transform.SetParent(parent, false);
        }
        Image image = newImageGo.AddComponent<Image>();
        return image;
    }

    /// <summary>
    /// 创建一个Text对象
    /// </summary>
    /// <param name="textStr"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    private static Text CreateText(string textStr, Transform parent = null) {
        GameObject newTextGo = new GameObject("Text");
        newTextGo.AddComponent<RectTransform>();
        //创建撤销点
        VIPUndo.CreateObjectUndo("Create Text Go", newTextGo);
        if (null != parent)
        {
            newTextGo.transform.SetParent(parent, false);
        }
        Text text = newTextGo.AddComponent<Text>();
        text.color = Color.black;
        text.text = textStr;
        return text;
    }

    /// <summary>
    /// 创建Button下的Text对象
    /// </summary>
    /// <param name="textStr"></param>
    /// <returns></returns>
    private static Text CreateButtonText(string textStr, Transform parent = null)
    {
        Text text = CreateText(textStr, parent);
        RectTransform textRect = text.GetComponent<RectTransform>();
        textRect.offsetMin = Vector2.zero;
        textRect.offsetMax = Vector2.zero;
        textRect.anchorMin = Vector2.zero;
        textRect.anchorMax = Vector2.one;
        text.fontSize = 14;
        text.alignment = TextAnchor.MiddleCenter;
        return text;
    }

    /// <summary>
    /// 创建InputField下的Text
    /// </summary>
    /// <param name="textStr"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    private static Text CreateInputFieldText(string textStr, Transform parent = null) {
        Text text = CreateText(textStr, parent);
        RectTransform textRect = text.GetComponent<RectTransform>();
        textRect.offsetMin = new Vector2(10, 6);
        textRect.offsetMax = new Vector2(-10, -7);
        textRect.anchorMin = Vector2.zero;
        textRect.anchorMax = Vector2.one;
        text.fontSize = 14;
        text.alignment = TextAnchor.UpperLeft;
        text.supportRichText = false;
        text.text = "";
        return text;
    }

    /// <summary>
    /// 创建InputField下的Placeholder
    /// </summary>
    /// <param name="textStr"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    private static Text CreateInputFieldPlaceholder(string textStr, Transform parent = null) {
        Text text = CreateText(textStr, parent);
        text.name = "Placeholder";
        RectTransform textRect = text.GetComponent<RectTransform>();
        textRect.offsetMin = new Vector2(10, 6);
        textRect.offsetMax = new Vector2(-10, -7);
        textRect.anchorMin = Vector2.zero;
        textRect.anchorMax = Vector2.one;
        text.fontStyle = FontStyle.Italic;
        text.fontSize = 14;
        text.alignment = TextAnchor.UpperLeft;
        text.color = new Color(0.2f, 0.2f, 0.2f, 0.5f);
        return text;
    }

    /// <summary>
    /// 设置父物体
    /// </summary>
    /// <param name="newGo"></param>
    /// <param name="canvas"></param>
    private static void SetParent(RectTransform newRect, Canvas canvas) {
        //获取选择的对象
        GameObject selectGo = Selection.activeGameObject;
        //如果选择了一个对象
        if (selectGo)
        {
            RectTransform selectRect = selectGo.GetComponent<RectTransform>();
            //选择的对象上有没有RectTransform属性，有则代表是UI类型的，反之则不是
            if (selectRect)
            {
                //第二个参数代表不受父坐标影响，回归原点
                newRect.SetParent(selectRect, false);
                return;
            }
        }
        //第二个参数代表不受父坐标影响，回归原点
        newRect.SetParent(canvas.transform, false);
    }

    /// <summary>
    /// 设置父物体
    /// </summary>
    /// <param name="newGo"></param>
    /// <param name="canvas"></param>
    private static void SetParent(Transform transform, Transform parent)
    {
        //获取选择的对象
        GameObject selectGo = Selection.activeGameObject;
        //如果选择了一个对象
        if (selectGo)
        {
            Transform selectTf = selectGo.transform;
            //选择的对象上有没有RectTransform属性，有则代表是UI类型的，反之则不是
            if (selectTf)
            {
                //第二个参数代表不受父坐标影响，回归原点
                transform.SetParent(selectTf, false);
                return;
            }
        }
        //第二个参数代表不受父坐标影响，回归原点
        transform.SetParent(parent, false);
    }

}
